package com.eve.eplus.main.Fragment;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.eve.eplus.R;
import com.eve.eplus.common.activity.StartActivity;
import com.eve.eplus.common.adapter.HomeMenuAdapter;
import com.eve.eplus.common.app.BaseApplication;
import com.eve.eplus.common.app.Constants;
import com.eve.eplus.common.app.SettingConstants;
import com.eve.eplus.common.task.ImageLoader;
import com.eve.eplus.main.AccountManagerActivity;
import com.eve.eplus.main.HomeActivity;
import com.eve.eplus.main.SetActivity;
import com.eve.eplus.main.ShareActivity;
import com.eve.eplus.main.logic.SettingUtility;
import com.eve.eplus.sns.weibo.UsersAPIDef;
import com.eve.eplus.sns.weibo.WeiboAPIGather;

/**
 * 主页面菜单Fragment
 * @author norsie
 * @create 12-11-29
 */
public class MenuFragment extends Fragment
{

    private ListView mListView;
    private ArrayList<HashMap<String, String>>  mAccountInfo;
    private HomeMenuAdapter adapter;
    
    
    public MenuFragment()
    {
        this(BaseApplication.SNS_ACCOUNT); // 要求必须有帐号 不然会出错
    }

    public MenuFragment(ArrayList<HashMap<String, String>>  accountInfo)
    {
        mAccountInfo=accountInfo;
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.activity_home_menu, null);
        view.findViewById(R.id.menu_manage_ll).setOnClickListener(onClickListener);
        view.findViewById(R.id.menu_send_ll).setOnClickListener(onClickListener);
        view.findViewById(R.id.sliMenuExit).setOnClickListener(onClickListener);
        view.findViewById(R.id.sliMenuSet).setOnClickListener(onClickListener);
        
        mListView=(ListView)view.findViewById(R.id.menu_lv);
        adapter=new HomeMenuAdapter(getActivity(), mAccountInfo);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(onItemClickListener);
        
        
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
      
    }
    
    @Override
    public void onResume() {
        super.onResume();
        mAccountInfo=BaseApplication.SNS_ACCOUNT;
        adapter.reflash(mAccountInfo);
    };
    
    OnClickListener onClickListener=new OnClickListener() {
        
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            switch (v.getId())
            {
                case R.id.menu_manage_ll:
                    Intent intent1=new Intent(getActivity(),AccountManagerActivity.class);
                    startActivity(intent1);
                    break;
                case R.id.menu_send_ll:
//                    switchFragment(new ShareFragment());
                    Intent intent3=new Intent(getActivity(),ShareActivity.class);
                    startActivity(intent3);
                    break;
                case R.id.sliMenuSet:
                    Intent intent2=new Intent(getActivity(),SetActivity.class);
                    startActivity(intent2);
                    break;
                case R.id.sliMenuExit:
                    BaseApplication.isExit=true;
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), StartActivity.class); 
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //清空栈
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    };

    OnItemClickListener onItemClickListener=new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id)
        {
            // TODO Auto-generated method stub
            Fragment newContent = null;
            newContent = new ContentFragment(BaseApplication.SNS_ACCOUNT.get(position));
            if (newContent != null)
                switchFragment(newContent);
            
        }
    };
  

    // the meat of switching the above fragment
    private void switchFragment(Fragment fragment)
    {
        if (getActivity() == null)
            return;
        HomeActivity homeAcitvity = (HomeActivity) getActivity();
        homeAcitvity.switchContent(fragment);
    }

}
